package com.kyzh.core.pager.weal.live

import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.google.android.material.button.MaterialButton
import com.gushenge.core.beans.LivePaiHangBean
import com.gushenge.core.requests.LiveRequest
import com.kyzh.core.R
import com.kyzh.core.databinding.FragmentLivesortBinding
import com.kyzh.core.databinding.ItemLivePaihangBinding
import com.tencent.qcloud.tuicore.component.fragments.BaseFragment
import org.jetbrains.anko.backgroundColor
import org.jetbrains.anko.support.v4.onRefresh
import org.jetbrains.anko.textColor

class LiveSortFragment :BaseFragment() {
    companion object{
        fun newInstance(roomid:String): LiveSortFragment {
            val args = Bundle()
            args.putString("roomid",roomid)
            val fragment = LiveSortFragment()
            fragment.arguments = args
            return fragment
        }
    }
    private var bind:FragmentLivesortBinding? = null
    private var roomid: String = ""
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        bind = FragmentLivesortBinding.inflate(inflater)
        return bind?.root
    }
    private var selectIndex : Int = 1
    private var myP : Int = 1
    private var adapter =Adapter()
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        roomid = arguments?.getString("roomid")!!
        btnState(bind?.btn1,bind?.btn2,0)
        LiveRequest.getLivePaiHangConfig {
            bind?.apply {
                include.rev.layoutManager = LinearLayoutManager(requireContext())
                include.rev.adapter = adapter
                include.swipe.onRefresh {
                    myP = 1
                    adapter.setNewInstance(null)
                    LiveRequest.getLivePaiHang(selectIndex,roomid){
                        adapter.setNewInstance(this.data)
                        include.swipe.isRefreshing =false
                    }
                }
                myP = 1
                LiveRequest.getLivePaiHang(selectIndex,roomid){
                    adapter.setNewInstance(this.data)
                    include.swipe.isRefreshing =false
                }
                adapter.setEmptyView(R.layout.empty)

                mbtg.addOnButtonCheckedListener { group, checkedId, isChecked ->
                    if(isChecked){
                        when(checkedId){
                            R.id.btn1->{
                                btnState(bind?.btn1,bind?.btn2,0)
                                myP = 1
                                selectIndex = 1
                                adapter.setNewInstance(null)
                                LiveRequest.getLivePaiHang(1,roomid){
                                    adapter.setNewInstance(this.data)
                                }
                            }
                            R.id.btn2->{
                                btnState(bind?.btn1,bind?.btn2,1)
                                myP = 1
                                selectIndex = 2
                                adapter.setNewInstance(null)
                                LiveRequest.getLivePaiHang(2,roomid){
                                    adapter.setNewInstance(this.data)
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    fun btnState(btn1 : MaterialButton?,btn2 :  MaterialButton?,state:Int){
        if(state == 0){
            btn1?.textColor = Color.WHITE
            btn1?.backgroundColor = Color.parseColor("#F77105")
            btn2?.textColor = Color.parseColor("#FF666666")
            btn2?.backgroundColor = Color.WHITE
        }else{
            btn1?.textColor = Color.parseColor("#FF666666")
            btn1?.backgroundColor = Color.WHITE
            btn2?.textColor = Color.WHITE
            btn2?.backgroundColor = Color.parseColor("#F77105")
        }
    }
    inner class Adapter:BaseQuickAdapter<LivePaiHangBean,BaseDataBindingHolder<ItemLivePaihangBinding>>(R.layout.item_live_paihang){
        override fun convert(
            holder: BaseDataBindingHolder<ItemLivePaihangBinding>,
            item: LivePaiHangBean
        ) {
            holder.dataBinding?.apply {
                data = item
                if(holder.absoluteAdapterPosition<9){
                    tvSort.text = "0${holder.absoluteAdapterPosition+1}"
                }else{
                    tvSort.text = "${holder.absoluteAdapterPosition+1}"
                }
            }
        }

    }
}